package com.tql.pandian.service.impl;

import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.datasource.annotation.Master;
import com.ruoyi.common.datasource.annotation.Slave;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.EnabledShopDto;
import com.tql.pandian.entity.*;
import com.tql.pandian.entity.dto.MainDto;
import com.tql.pandian.entity.dto.SimpleDto;
import com.tql.pandian.entity.vo.HillKingDeeVo;
import com.tql.pandian.mapper.CommonMapper;
import com.tql.pandian.mapper.TqlStapProlossMapper;
import com.tql.pandian.service.ICommonService;
import com.tql.pandian.util.CommonUtilHll;
import com.tql.pandian.vo.TqlPandianPeriodVO;
import com.tql.pandian.vo.TqlStapProlossItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CommonService implements ICommonService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private TqlStapProlossMapper tqlStapProlossMapper;
    @Override
    //查询那些门店已经开通了本系统的服务 ,kingdee对应的门店编码
    public List<String> getEnabledShop() {
        List<String> enabledShops = redisService.getCacheList("enabledShops");
        if (CollectionUtils.isEmpty(enabledShops)) {
            List<String> shops = commonMapper.getEnabledShop();
            if (CollectionUtils.isEmpty(shops)) return null;
            //redis持久化
            redisService.setCacheList("enabledShops", shops);
            return shops;
        } else {
            return enabledShops;
        }

    }

//    public String getShopDeptCode(Long userId)
//    {
//
//    }


    /**
     *   select shopCode,kingdeeDeptCode
     *         from tql_hll_kingdee
     *         where flag=1;
     * @param fnumber
     * @return
     */
    @Master
    public String getEnabledHllShopIdByKingdeeFnumber(String fnumber) {
        Map<String, String> map = redisService.getCacheMap("hllKingShopIdMap");
        if(CollectionUtils.isEmpty(map))
        {
            List<EnabledShopDto> list =getEnabledHllShop();
            if (CollectionUtils.isEmpty(list)) return null;
            Map<String, String> map1 = list.stream()
                    .collect(Collectors.toMap(EnabledShopDto::getKingdeeDeptCode, EnabledShopDto::getShopCode));
            //redis持久化
            redisService.setCacheMap("hllKingShopIdMap", map1);
            return map1.get(fnumber);
        }
        else {

            return map.get(fnumber);
        }

    }

    private List<EnabledShopDto> getEnabledHllShop() {
        return commonMapper.getEnabledHllShop();
    }

    @Override
    @Master
    public TqlPandianPeriodVO findEnableGenPharse(String pandianCode) {
        return commonMapper.findEnableGenPharse(pandianCode);
    }

    @Override
    @Master
    public TqlPandianPeriodVO findEnableGenPharseUpper(String deptCode, String pcode) {
        return commonMapper.findEnableGenPharseUpper(deptCode, pcode);
    }

    @Override
    @Slave
    public List<FoodDetailReportDayBean> findFoodReportDay(String tableName, Long shopId, Long startDate, Long endDate)
    {
        return  commonMapper.findFoodReportDay(tableName,shopId,startDate,endDate);
    }

    @Override
    public int batchInsertFoodStatByReportDayAndShop(List<FoodDetailReportDayBean> list,String fnumber,String month,String pandianCode,Long starttime,Long endtine)
    {
        //FoodPeriodStat
        return  commonMapper.batchInsertFoodStatByReportDayAndShop(list,fnumber,month,pandianCode,starttime,endtine);
    }



    @Override
    public int batchInsertStapSaleChangeByPeriod(List<TqlStapSaleChangeEntity> list) {
        if(CollectionUtils.isEmpty(list))return 0;
        int size=list.size();
        if(size>200)
        {
            int pageNum=size/50;
            int startIndex=0;
            for(int i=0;i<=pageNum;i++)
            {
                int endIndex=(i+1)*50;
                if(i==pageNum)endIndex=size;
                List<TqlStapSaleChangeEntity> subList = list.subList(startIndex,endIndex);
                commonMapper.batchInsertStapSaleChangeByPeriod(subList);
                startIndex=startIndex+50;
            }

        }
        else {
            commonMapper.batchInsertStapSaleChangeByPeriod(list);
        }
        return size;
    }

    @Override
    public List<FoodBomEntity> findFoodBomByFoodCode(FoodDetailReportDayBean bean) {
        return commonMapper.selectFoodAndFoodBomByFoodIdAndDeptCode(bean.getFoodCode(),bean.getFnumber());
    }

    @Override
    public String findShopDeptCodeByUserId(Long userId) {
        String prex= CommonUtilHll.SHOPNAME_BY_USERID;
        Map<String, String> map = redisService.getCacheMap(prex);
        String shopName=null==map?null:map.get(userId.toString());
        if(null!=map&&map.size()>0&&StringUtil.isNullOrEmpty(shopName)) {
            return shopName;
        } else {
            //假如一个店一个账号的话
            String deptCodeByUserId=  commonMapper.findDeptCodeByUserId(userId);
            if(StringUtil.isNullOrEmpty(shopName)) return null;
            //map不为空的情况下，放入这个(k,v),更新redis
            String str=prex+"-"+userId;
            map.put(str,deptCodeByUserId);
            redisService.setCacheMap(prex,map);
            return deptCodeByUserId;
        }
    }

    @Override
    public List<HillKingDeeVo> HillKingDees() {
        List<HillKingDeeVo> hillKingDeeVos = commonMapper.selectHillKings();
        return hillKingDeeVos;
    }

    @Override
    public String findDeptCode(Long userId) {
        return commonMapper.findDeptCodeByUserId(userId);
    }

    @Override
    @Master
    public List<TqlStapProlossItemVO> findStapPeriodProloss(String pandianCode, Long std, Long etd, Integer type) {
        return commonMapper.selectStapPeriodProloss(pandianCode, std, etd, type);
    }

    @Override
    @Slave
    public TqlStapProlossItemVO findTqlStapOutChange(String tableName, String deptCode, String stapCode, String reportDate) {
        return commonMapper.findTqlStapOutChange(tableName, deptCode, stapCode, reportDate);
    }

    @Override
    public void insetBatchProlossData(List<TqlStapProloss> prolossData) {
        commonMapper.insetBatchProlossData(prolossData);
    }

    @Override
    public void insetBatchProlossItemData(List<TqlStapProlossItem> prolossItemData) {
        commonMapper.insetBatchProlossItemData(prolossItemData);
    }

    @Override
    public void deleteItemByPandinacode(String pandianCode) {
        commonMapper.deleteItemByPandinacode(pandianCode);
    }

    /**
     * tql_stap_proloss
     * @param pandianCode
     */
    @Override
    @Master
    public void deleteByPandinacode(String pandianCode) {
        commonMapper.deleteByPandinacode(pandianCode);
    }

    @Override
    @Slave
    public List<TqlHllShopBillFoodPeriod> getTqlHllShopBillFoodReportDayList(String tablename,String pandianCode,String yyyymm, Long shopId, Long std, Long etd) {
        return commonMapper.selectTqlHllShopBillFoodReportDayList(tablename, pandianCode, yyyymm, shopId, std, etd);
    }

    @Override
    @Master
    public void insertTqlHllShopBillFoodPeriod(String pandianCode, List<TqlHllShopBillFoodPeriod> list) {
        commonMapper.delectTqlHllShopBillFoodPeriodByPandianCode(pandianCode);
        commonMapper.insertTqlHllShopBillFoodPeriod(list);
    }

    @Override
    @Master
    public List<String> findfoodCode(String fnumber, String[] arr1) {
       return commonMapper.findfoodCode(fnumber,arr1);
    }

    @Override
    @Master
    public int insertBomcardAnalysAndProfit(List<FoodAndBom> list) {
        return commonMapper.insertBomcardAnalysAndProfit(list);
    }

    @Override
    @Master
    public void insetBatchProlossDetailData(List<ProlossDetialDto> list) {
        commonMapper.insetBatchProlossDetailData(list);
    }

    @Override
    @Slave
    public List<TqlStapProlossItemVO> selectStapInChange(String tabName, String deptCode, Object o, Long std, Long etd) {
        return tqlStapProlossMapper.selectStapInChange(tabName, deptCode,null, std, etd);
    }

    @Override
    @Master
    public void deleteProlossDetailByPandinacode(String pandianCode) {
           commonMapper.deleteProlossDetailByPandinacode(pandianCode);
    }

    @Override
    public void deleteBomcardAnalys(String pandianCode) {
        commonMapper.deleteBomcardAnalys(pandianCode);
    }

    @Override
    @Slave
    public List<SimpleDto> findStapAndDeptFromInAndOut(int month, String fnumber) {
        String inTableName= CommonUtil.TABLE_IN_CHANGE+month;
        String outTalbeName=CommonUtil.TABLE_OUT_CHANGE+month;
      return  commonMapper.findStapAndDeptFromInAndOut(inTableName,outTalbeName,fnumber);
    }

    @Override
    public List<SimpleDto> findStapAndDeptFromPandianData(String pandianCode) {
        return  commonMapper.findStapAndDeptFromPandianData(pandianCode);
    }

    @Override
    @Slave
    public List<SimpleDto> findInDetailByStapAndDeptForTransferout(int month, List<String> list) {
        String inTableName= CommonUtil.TABLE_IN_CHANGE+month;

        return  commonMapper.findInDetailByStapAndDeptForTransferout(inTableName,list);
    }

    @Override
    @Slave
    public List<SimpleDto> findInDetailByStapAndDeptForTransferIn(int month, List<String> list) {
        String inTableName= CommonUtil.TABLE_IN_CHANGE+month;

        return  commonMapper.findInDetailByStapAndDeptForTransferIn(inTableName,list);
    }

    @Override
    @Slave
    public List<SimpleDto> findInDetailByStapAndDeptForPurcharse(int month, List<String> list) {
        String inTableName= CommonUtil.TABLE_IN_CHANGE+month;

        return  commonMapper.findInDetailByStapAndDeptForPurcharse(inTableName,list);
    }

    @Override
    @Slave
    public List<SimpleDto> findInDetailByStapAndDeptForIncase(int month,List<String> list) {
        String inTableName = CommonUtil.TABLE_IN_CHANGE + month;

        return commonMapper.findInDetailByStapAndDeptForIncase(inTableName, list);
    }

    @Override
    @Slave
    public List<SimpleDto> findOutDetailByDeptStap(int month, List<String> list, Long std, Long etd) {
        String outTableName = CommonUtil.TABLE_OUT_CHANGE + month;

        return commonMapper.findOutDetailByStapAndDept(outTableName, list,std,etd);
    }

    @Override
    @Master
    public List<SimpleDto> findLastPandianDetail(String pandianCode, List<String> list) {
        return commonMapper.findLastPandianDetail(pandianCode, list);
    }

    @Override
    @Master
    public List<SimpleDto> findThisPandianDetail(String pandianCode, List<String> list) {
        return commonMapper.findThisPandianDetail(pandianCode, list);
    }

    @Override
    @Master
    @Transactional
    public void batch(List<MainDto> mains, List<SimpleDto> union) {
        commonMapper.batchInsertProloss(mains);
        commonMapper.batchInsertProlossDetail(union);
    }

    @Override
    @Slave
    @Transactional
    public void batchBillFoodReport(int month,String shopID) {
        String billFoodReportTableName=CommonUtil.TableName_ShopBillFoodReportDayDetail+month;
        String billFoodTableName=CommonUtil.TableName_ShopBillFoodDetail+month;

        commonMapper.saveShopBillReport1(billFoodReportTableName,billFoodTableName,shopID);
        commonMapper.updateShopBill1(billFoodTableName,shopID);
    }

    @Override
    public List<TqlHllShopBillFoodPeriod> selectBillFoodPeriod(String pandianCode) {
        return commonMapper.selectBillFoodPeriod(pandianCode);
    }

    @Override
    public List<TqlStapProloss> selectProloss(String pandianCode) {
        return commonMapper.selectProloss(pandianCode);
    }

    @Override
    @Master
    public List<FoodAndBom> findBomCardParse(String pandianCode, String type) {
        return commonMapper.findBomCardParse(pandianCode,type);
    }

    @Override
    @Master
    public List<FoodAndBom> findFoodAndBom(String fnumber) {
      return   commonMapper.findFoodAndBom(fnumber);
    }

    @Override
    @Master
    public List<FoodAndBom> findBillFoodPeriod( String pandianCode, String[] foodCodes) {
        return   commonMapper.findBillFoodPeriod(pandianCode,foodCodes);
    }


}
